package com.hy.main.demo.io;

import org.junit.Test;

import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author shy Boy
 * @Date 2022/4/25 - 04 - 25 - 23:24
 * @Description: com.hy.main.demo.file
 * @version: 1.0
 */
public class FileReaders {

    /**
     * 字符输入流
     */
    @Test
    public void fileReaders() throws IOException {
        String filePath = "e:\\myFile.txt";
        FileReader fileReader = null;
        String s;
        // 定义一个字符读取缓冲区
        char[] buffer = new char[1024];
        // 接收每次reader到的字符长度
        int readerLength;
        try {
            fileReader = new FileReader(filePath);
            while ((readerLength = fileReader.read(buffer)) != -1) {
                // 字符数组转换为字符串
                String info = new String(buffer, 0, readerLength);
                Pattern p = Pattern.compile("\\s*|\t|\r|\n");
                /*   \n 回车(\u000a)   \t 水平制表符(\u0009)   \s 空格(\u0008)   \r 换行(\u000d)   */
                Matcher m = p.matcher(info);
                s = m.replaceAll("");
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            assert fileReader != null;
            fileReader.close();

        }
    }
}
